<template>
  <div class="user-info">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="82px"
      class="demo-ruleForm"
    >
      <el-form-item prop="pass">
        <div id="head-box" class="f-c-c">
          <el-upload
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <el-avatar :size="150" :src="imgSrc"></el-avatar>
          </el-upload>
        </div>
      </el-form-item>
      <el-form-item label="手机号码：" prop="age">
        <el-input type="tel" v-model.number="ruleForm.age" disabled></el-input>
      </el-form-item>
      <el-form-item label="昵称：" prop="pass">
        <el-input v-model="ruleForm.pass" placeholder="请输入昵称"></el-input>
      </el-form-item>
      <el-form-item label="密码：" prop="checkPass">
        <el-input
          type="password"
          v-model="ruleForm.checkPass"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item label="姓名：" prop="pass">
        <el-input v-model="ruleForm.pass" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="性别：" prop="pass">
        <el-radio-group v-model="radio1">
          <el-radio-button label="未知"></el-radio-button>
          <el-radio-button label="男"></el-radio-button>
          <el-radio-button label="女"></el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="出生日期：" prop="age">
        <el-date-picker
          v-model="birthday"
          width="244px"
          type="date"
          style="width: 100%"
          placeholder="请选择日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >提交修改</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Update",
  data() {
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("年龄不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error("请输入数字值"));
        } else {
          if (value < 18) {
            callback(new Error("必须年满18岁"));
          } else {
            callback();
          }
        }
      }, 1000);
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      radio1: "未知",
      imgSrc: require("@/public/img/defaultAvatar.gif"),
      birthday: "",
      imageUrl: "../../public/img/default_head_img.gif",
      ruleForm: {
        pass: "",
        checkPass: "",
        age: "",
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        age: [{ validator: checkAge, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>
<style lang="scss" scoped>
.el-form {
  padding: 0 70px;
}
#head-box {
}
</style>